<?php

namespace app\controllers;

use app\models\InPoint;
use app\models\OldinPoint;
use app\models\OldoutPoint;
use app\models\Oldpagerank;
use app\models\OutPoint;
use app\models\Prediction;
use app\models\Presale;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use app\models\Olddata;
use app\models\Sale;
use app\models\Oldsale;
use app\models\Pagerank;

class CompareController extends Controller
{

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['index','login','logout','detailc','adetailc','detailm','adetailm','cookround','cookcount','airline','pagerank',
                    'ajaxpagerank','ajaxsale','ajaxcount','agaxtopr','ajaxtopc','ajaxagents','in','out',
                    'ajaxin','ajaxout','ajaxacount','ajaxamoney'],
                'rules' => [
                    [
                        'actions' => ['index','logout','detailc','adetailc','detailm','adetailm','cookround','cookcount','airline','pagerank',
                            'ajaxpagerank','ajaxsale','ajaxcount','agaxtopr','ajaxtopc','ajaxagents','in','out',
                            'ajaxin','ajaxout','ajaxacount','ajaxamoney'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                    [
                        'actions' => ['login'],
                        'allow' => true,
                        'roles' => ['?'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }


    public function actionAdmin()
    {
        if(\Yii::$app->request->isPost)
        {
            $airline=\Yii::$app->request->post('airline');
            return $this->render('index',
                ['airline'=>$airline]
            );
        }
        else
        {
            return $this->render('airline_search');
        }
    }



    /**
     * Displays about page.
     *
     * @return string
     */
    public function actionAdetailc()//全市场销售量对比
    {
        $role=$this->getRole();
        if(\Yii::$app->request->isPost)
        {
            if($this->isAdmin())
            {
                $airline=\Yii::$app->request->post('airline');
            }
            else
            {
                $airline=$role;
            }
            return $this->render('adetailc',
                [
                    'airline'=>$airline,
                ]);
        }
        else
        {
            if($this->isAdmin())
            {
                $airline='C1';
            }
            else
            {
                $airline=$role;
            }
            return $this->render('adetail_searchc',['airline'=>$airline]);
        }
    }


    public function actionAdetailm()//全市场销售额对比
    {
        $role=$this->getRole();
        if(\Yii::$app->request->isPost)
        {
            if($this->isAdmin())
            {
                $airline=\Yii::$app->request->post('airline');
            }
            else
            {
                $airline=$role;
            }
            return $this->render('adetailm',
                [
                    'airline'=>$airline,
                ]);
        }
        else
        {
            if($this->isAdmin())
            {
                $airline='C1';
            }
            else
            {
                $airline=$role;
            }
            return $this->render('adetail_searchm',['airline'=>$airline]);
        }
    }

    public function actionDetailc()//代理人销售量对比
    {
        $role=$this->getRole();
        if(\Yii::$app->request->isPost)
        {
            if($this->isAdmin())
            {
                $airline=\Yii::$app->request->post('airline');
            }
            else
            {
                $airline=$role;
            }
            $agent=\Yii::$app->request->post('agent');
            return $this->render('detailc',
                [
                    'agent'=>$agent,
                    'airline'=>$airline,
                ]);
        }
        else
        {
            if($this->isAdmin())
            {
                $airline='C1';
            }
            else
            {
                $airline=$role;
            }
            return $this->render('detail_searchc',['airline'=>$airline]);
        }
    }

    public function actionDetailm()//代理人销售额对比
    {
        $role=$this->getRole();
        if(\Yii::$app->request->isPost)
        {
            if($this->isAdmin())
            {
                $airline=\Yii::$app->request->post('airline');
            }
            else
            {
                $airline=$role;
            }
            $agent=\Yii::$app->request->post('agent');
            return $this->render('detailm',
                [
                    'agent'=>$agent,
                    'airline'=>$airline,
                ]);
        }
        else
        {
            if($this->isAdmin())
            {
                $airline='C1';
            }
            else
            {
                $airline=$role;
            }
            return $this->render('detail_searchm',['airline'=>$airline]);
        }
    }


    public function actionCookround()//代理人销售额Top饼图
    {
        $role=$this->getRole();
        if(\Yii::$app->request->isPost)
        {
            if($this->isAdmin())
            {
                $airline=\Yii::$app->request->post('airline');
            }
            else
            {
                $airline=$role;
            }
            $dayid=\Yii::$app->request->post('dayid');
            return $this->render('cook_r',
                [
                    'airline'=>$airline,
                    'dayid'=>$dayid
                ]);
        }
        else
        {
            if($this->isAdmin())
            {
                $airline='C1';
            }
            else
            {
                $airline=$role;
            }
            return $this->render('cook_searchr',['airline'=>$airline]);
        }

    }



    public function actionCookcount()//代理人销售量Top饼图
    {
        $role=$this->getRole();
        if(\Yii::$app->request->isPost)
        {
            if($this->isAdmin())
            {
                $airline=\Yii::$app->request->post('airline');
            }
            else
            {
                $airline=$role;
            }
            $dayid=\Yii::$app->request->post('dayid');
            return $this->render('cook_c',
                [
                    'airline'=>$airline,
                    'dayid'=>$dayid
                ]);
        }
        else
        {
            if($this->isAdmin())
            {
                $airline='C1';
            }
            else
            {
                $airline=$role;
            }
            return $this->render('cook_searchc',['airline'=>$airline]);
        }

    }



    public function actionAjaxsale($airline)//航空公司销售情况
    {
       // $money=Sale::findBySql('SELECT day_id,round,cnt FROM sale WHERE sale_nbr=\''.$airline.'\' ORDER BY day_id ASC;')->asArray()->all();
        $money=Presale::find()->where(['sale_nbr'=>$airline])->orderBy(['day_id'=>SORT_ASC])->asArray()->all();
        $X=[];
        $mY=[];
        $cY=[];
        $data=[];
        if($money!=null)
        {
            foreach ($money as $v)
            {
                $X[]=$v['day_id'];
                $mY[]=$v['round'];
                $cY[]=$v['cnt'];
            }
            $data['X']=$X;
            $data['mY']=$mY;
            $data['cY']=$cY;
        }
        $data=json_encode($data);
        return $data;
    }

    public function actionAjaxacount()//全市场对比销售量
    {
        if($this->isAdmin())
        {
            $airline=\Yii::$app->request->post('airline');
        }
        else
        {
            $airline=$this->getRole();
        }
        $init=Oldsale::find()->where(['sale_nbr'=>$airline])->andWhere(['>=','day_id',81])->orderBy(['day_id'=>SORT_ASC])->asArray()->all();
        $new=Presale::find()->where(['sale_nbr'=>$airline])->andWhere(['>=','day_id',81])->orderBy(['day_id'=>SORT_ASC])->asArray()->all();
        $X=[];$iY=[];$nY=[];$data=[];
        foreach ($init as $v)
        {
            $X[]=$v['day_id'];
            $iY[]=$v['cnt'];
        }
        foreach ($new as $v)
        {
            $nY[]=$v['cnt'];
        }
        $data['X']=$X;
        $data['iY']=$iY;
        $data['nY']=$nY;
        $data['airline']=$airline;
        $data=json_encode($data);
        return $data;
    }

    public function actionAjaxamoney()//全市场对比销售额
    {
        if($this->isAdmin())
        {
            $airline=\Yii::$app->request->post('airline');
        }
        else
        {
            $airline=$this->getRole();
        }
        $init=Oldsale::find()->where(['sale_nbr'=>$airline])->andWhere(['>=','day_id',81])->orderBy(['day_id'=>SORT_ASC])->asArray()->all();
        $new=Presale::find()->where(['sale_nbr'=>$airline])->andWhere(['>=','day_id',81])->orderBy(['day_id'=>SORT_ASC])->asArray()->all();
        $X=[];$iY=[];$nY=[];$data=[];
        foreach ($init as $v)
        {
            $X[]=$v['day_id'];
            $iY[]=$v['round'];
        }
        foreach ($new as $v)
        {
            $nY[]=$v['round'];
        }
        $data['X']=$X;
        $data['iY']=$iY;
        $data['nY']=$nY;
        $data['airline']=$airline;
        $data=json_encode($data);
        return $data;
    }

    public function actionAjaxcount()//代理人对比销售量
    {
        if($this->isAdmin())
        {
            $airline=\Yii::$app->request->post('airline');
        }
        else
        {
            $airline=$this->getRole();
        }
        $agent=\Yii::$app->request->post('agent');
        $init=Olddata::find()->where(['sale_nbr'=>$airline,'buy_nbr'=>$agent])->andWhere(['>=','day_id',81])->orderBy(['day_id'=>SORT_ASC])->asArray()->all();
        $new=Prediction::find()->where(['sale_nbr'=>$airline,'buy_nbr'=>$agent])->andWhere(['>=','day_id',81])->orderBy(['day_id'=>SORT_ASC])->asArray()->all();
        $X=[];$iY=[];$nY=[];$data=[];
        foreach ($init as $v)
        {
            $X[]=$v['day_id'];
            $iY[]=$v['cnt'];
        }
        foreach ($new as $v)
        {
            $nY[]=$v['cnt'];
        }
        $data['X']=$X;
        $data['iY']=$iY;
        $data['nY']=$nY;
        $data['airline']=$airline;
        $data['agent']=$agent;
        $data=json_encode($data);
        return $data;
    }

    public function actionAjaxmoney()//对比销售额
    {
        if($this->isAdmin())
        {
            $airline=\Yii::$app->request->post('airline');
        }
        else
        {
            $airline=$this->getRole();
        }
        $agent=\Yii::$app->request->post('agent');
        $init=Olddata::find()->where(['sale_nbr'=>$airline,'buy_nbr'=>$agent])->andWhere(['>=','day_id',81])->orderBy(['day_id'=>SORT_ASC])->asArray()->all();
        $new=Prediction::find()->where(['sale_nbr'=>$airline,'buy_nbr'=>$agent])->andWhere(['>=','day_id',81])->orderBy(['day_id'=>SORT_ASC])->asArray()->all();
        $X=[];$iY=[];$nY=[];$data=[];
        foreach ($init as $v)
        {
            $X[]=$v['day_id'];
            $iY[]=$v['round'];
        }
        foreach ($new as $v)
        {
            $nY[]=$v['round'];
        }
        $data['X']=$X;
        $data['iY']=$iY;
        $data['nY']=$nY;
        $data['airline']=$airline;
        $data['agent']=$agent;
        $data=json_encode($data);
        return $data;
    }

    public function actionAjaxtopr()//代理人在航空公司的销售额top
    {
        if($this->isAdmin())
        {
            $airline=\Yii::$app->request->post('airline');
        }
        else
        {
            $airline=$this->getRole();
        }
        $dayid=\Yii::$app->request->post('dayid');
        $top10_num=0;
        $total=0;
//        $result=Sale::findBySql('SELECT * FROM olddata WHERE sale_nbr=\''.$airline.'\' AND day_id=\''.$dayid.'\' ORDER BY round DESC LIMIT 0,10;')->asArray()->all();
//        $r=Sale::findBySql('SELECT * FROM sale WHERE sale_nbr=\''.$airline.'\' AND day_id=\''.$dayid.'\';')->asArray()->one();
        $dayid=(int)$dayid;
        $result=Prediction::find()->where(['sale_nbr'=>$airline,'day_id'=>$dayid])->orderBy(['round'=>SORT_DESC])->limit(10)->offset(0)->asArray()->all();
        $r=Presale::find()->where(['sale_nbr'=>$airline,'day_id'=>$dayid])->asArray()->one();


        if(isset($r['round']))
        {
            $total=$r['round'];
        }
        $agent=[];
        $data=[];
        $r=[];
        foreach ($result as $v)
        {
            $data[]=['value'=>$v['round'], 'name'=>$v['buy_nbr']];
            $agent[]=$v['buy_nbr'];
            $top10_num=$top10_num+$v['round'];
        }
        $other=(double)$total-(double)$top10_num;
        $data[]=['value'=>$other, 'name'=>'其他'];
        $agent[]='其他';
        $r['data']=$data;
        $r['agent']=$agent;
        $r['airline']=$airline;
        $r=json_encode($r);
        return $r;
    }


    public function actionAjaxtopc()//代理人在航空公司的销售量top
    {
        if($this->isAdmin())
        {
            $airline=\Yii::$app->request->post('airline');
        }
        else
        {
            $airline=$this->getRole();
        }
        $dayid=\Yii::$app->request->post('dayid');
        $top10_num=0;
        $total=0;
//        $result=Sale::findBySql('SELECT * FROM olddata WHERE sale_nbr=\''.$airline.'\' AND day_id=\''.$dayid.'\' ORDER BY cnt DESC LIMIT 0,10;')->asArray()->all();
//        $r=Sale::findBySql('SELECT * FROM sale WHERE sale_nbr=\''.$airline.'\' AND day_id=\''.$dayid.'\';')->asArray()->one();
        $dayid=(int)$dayid;
        $result=Prediction::find()->where(['sale_nbr'=>$airline,'day_id'=>$dayid])->orderBy(['cnt'=>SORT_DESC])->limit(10)->offset(0)->asArray()->all();
        $r=Presale::find()->where(['sale_nbr'=>$airline,'day_id'=>$dayid])->asArray()->one();
        if(isset($r['cnt']))
        {
            $total=$r['cnt'];
        }
        $agent=[];
        $data=[];
        $r=[];
        foreach ($result as $v)
        {
            $data[]=['value'=>$v['cnt'], 'name'=>$v['buy_nbr']];
            $agent[]=$v['buy_nbr'];
            $top10_num=$top10_num+$v['cnt'];
        }
        $other=(double)$total-(double)$top10_num;
        $data[]=['value'=>$other, 'name'=>'其他'];
        $agent[]='其他';
        $r['data']=$data;
        $r['agent']=$agent;
        $r['airline']=$airline;
        $r=json_encode($r);
        return $r;
    }

    public function actionAjaxairlines()//ajax获取所有航空公司（分页显示）
    {
        $pageSize=25;
        $page=\Yii::$app->request->post('page',0);
        if($page<0)
            $page=0;
        $sql='SELECT DISTINCT sale_nbr FROM sale WHERE sale_nbr LIKE \'C%\' LIMIT '.$page*$pageSize.','.$pageSize.';';
        $airlines=Sale::findBySql($sql)->asArray()->all();
        $data=[];
        $data['airlines']=$airlines;
        $data['page']=$page;
        $data=json_encode($data);
        return $data;
    }

    public function actionAjaxallagents()//ajax获取所有代理人（分页显示）
    {
        $pageSize=25;
        $page=\Yii::$app->request->post('page',0);
        if($page<0)
            $page=0;
        $sql='SELECT DISTINCT sale_nbr FROM sale WHERE sale_nbr LIKE \'O%\' LIMIT '.$page*$pageSize.','.$pageSize.';';
        $agents=Sale::findBySql($sql)->asArray()->all();
        $data=[];
        $data['agents']=$agents;
        $data['page']=$page;
        $data=json_encode($data);
        return $data;
    }

    public function getRole()
    {
        $roles=\Yii::$app->authManager->getRolesByUser(\Yii::$app->user->id);
        $r=array_keys($roles);
        $role=$r[0];
        return $role;
    }

    public function isAdmin()
    {
        return \Yii::$app->authManager->checkAccess(Yii::$app->user->id, 'admin');
    }

}
